package cn.fl.capital.controller;

import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.capital.condition.CapPrjArInfoQC;
import cn.fl.capital.service.ICapPrjArInfoService;
import cn.fl.capital.vo.CapPrjArInfoTableVO;
import cn.fl.capital.vo.CapPrjArInfoVO;
import cn.fl.customer.service.ICstBscInfoService;
import cn.fl.framework.base.context.CurrentThreadContext;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 信审项目剩余敞口信息 控制层
 */
@Controller
@RequestMapping("/exposure/capPrjArInfo")
public class CapPrjArInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(CapPrjArInfoController.class);

	/**
	 * CapPrjArInfo 服务层BEAN
	 */
	@Autowired
	private ICapPrjArInfoService capPrjArInfoService;

	@Autowired
	private ICstBscInfoService cstBscInfoService;
	/**
	 * 敞口统计结果页面，支持事业部条件查询
	 * @param prjId
	 * @param buOrgCd
	 * @param isShowCondition 页面上查询条件是否显示  1:是 0：否
	 * @param useCache 是否使用缓存数据 1：是 0：否
	 * @return
	 */
	@RequestMapping(value = "/toArInfoTable.do", method = { RequestMethod. GET})
	public ModelAndView toArInfoTable(@RequestParam Long prjId,@RequestParam(required = false) String buOrgCd,@RequestParam(required = false,defaultValue = "0") String isShowCondition,
									  @RequestParam(required = false,defaultValue = "1") String useCache){
		ModelAndView mv=new ModelAndView("capital/capprjarinfo/capPrjArInfoB");
		// 事业部
		KeyValueVO org1 = new KeyValueVO();
		org1.setKey("00000001");
		org1.setValue("重卡事业部");
		KeyValueVO org2 = new KeyValueVO();
		org2.setValue("轻卡事业部");
		org2.setKey("00000014");
		List<KeyValueVO> orgList = new ArrayList<>();
		orgList.add(org1);
		orgList.add(org2);
		mv.addObject("buOrgCdList", JSON.toJSONString(orgList));
		mv.addObject("prjId",prjId);
		mv.addObject("buOrgCd",buOrgCd);
		mv.addObject("isShowCondition",isShowCondition);
		mv.addObject("useCache",useCache);
		return mv;
	}
	@RequestMapping(value = "/initTable.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initTable(@RequestParam Long prjId,@RequestParam(required = false) String buOrgCd,
	@RequestParam(required = false,defaultValue = "1") String useCache){
		List<CapPrjArInfoTableVO> list = capPrjArInfoService.selectTableList(prjId, buOrgCd,useCache);
		ListPageVO<CapPrjArInfoTableVO> listPage = new ListPageVO<CapPrjArInfoTableVO>(list,list.size());
		return listPage;

	}

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toExposureCapPrjArInfoPage.do", method = { RequestMethod. GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("capital/capprjarinfo/capPrjArInfo");
		view.addObject("prjId",request.getParameter("prjId"));
		view.addObject("exposureSource",request.getParameter("exposureSource"));
		BaseLogUtils.info(logger, "toListPage", "跳转到列表页面.", CurrentThreadContext.getCurrentUserName());
		return view;
	}



	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		//敞口统计数据源
		CapPrjArInfoQC condition = new CapPrjArInfoQC();
		String exposureSource = request.getParameter("exposureSource");
		String prjIdStr = request.getParameter("prjId");
		String buOrgCd = request.getParameter("buOrgCd");
		if(StringUtils.isEmpty(buOrgCd)){
			buOrgCd="0000";
		}
		if(StringUtils.isBlank(prjIdStr)){
			BaseLogUtils.info(logger, "initList","项目ID为空！", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		Long prjId = Long.valueOf(prjIdStr);
		condition.setPrjId(Long.valueOf(prjId));
		condition.setBuOrgCd(buOrgCd);
		List<CapPrjArInfoVO> list = new ArrayList<CapPrjArInfoVO>();
		if (StringUtils.isNotEmpty(exposureSource)) {
			if ("1".equals(exposureSource)) {
				//调用mongodb接口
				list = this.capPrjArInfoService.searchList(condition,true);
			} else if ("2".equals(exposureSource)) {
				//数据库取数据
				list = this.capPrjArInfoService.searchList(condition,false);

			}
		}
		ListPageVO<CapPrjArInfoVO> listPage = new ListPageVO<CapPrjArInfoVO>(list,list.size());
		BaseLogUtils.info(logger, "initList", "查询数据完成. DATA:" + JSONObject.toJSONString(list), CurrentThreadContext.getCurrentUserName());
		return listPage;
	}

}

